package handlers

import (
	"cargo-stat/cache"
	"cargo-stat/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func PopeAdd(c *gin.Context) {
	popeNo := c.PostForm("pope_no")
	popeName := c.PostForm("pope_name")
	popeArea := c.PostForm("pope_area")
	popeUser := c.PostForm("pope_user")
	if popeNo == "" || popeName == "" || popeArea == "" || popeUser == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}

	pope := models.Pope{
		PopeNo:   popeNo,
		PopeName: popeName,
		PopeArea: popeArea,
		PopeUser: popeUser,
	}
	err := models.DB.Create(&pope).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	orderNo := c.PostForm("order_no")
	orderBoss := c.PostForm("order_boss")
	orderNum, _ := strconv.Atoi(c.PostForm("order_num"))
	orderPrice, _ := strconv.Atoi(c.PostForm("order_price"))
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	if orderNo == "" || orderBoss == "" || orderNum == 0 || orderPrice == 0 || pid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	order := models.Order{
		OrderNo:    orderNo,
		OrderBoss:  orderBoss,
		OrderNum:   orderNum,
		OrderPrice: orderPrice,
		Pid:        pid,
	}
	err := models.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PopeOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	/*
			SELECT
		  orders.id AS OrderId,
		  popes.pope_no AS PopeNo,
		  popes.pope_name AS PopeName,
		  popes.pope_area AS PopeArea,
		  popes.pope_user AS PopeUser,
		  orders.order_no AS OrderNo,
		  orders.order_boss AS OrderBoss,
		  orders.order_num AS OrderNum,
		  orders.order_price AS OrderPrice
		FROM
		  orders
		  LEFT JOIN popes ON orders.pid = popes.id
	*/
	var popeOrder []models.PopeOrder
	redis_key := "list_popeOrder" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderId",
				"popes.pope_no AS PopeNo",
				"popes.pope_name AS PopeName",
				"popes.pope_area AS PopeArea",
				"popes.pope_user AS PopeUser",
				"orders.order_no AS OrderNo",
				"orders.order_boss AS OrderBoss",
				"orders.order_num AS OrderNum",
				"orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN popes ON orders.pid = popes.id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&popeOrder)
		jsonData, _ := json.Marshal(popeOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &popeOrder)
	}
	c.JSON(200, gin.H{
		"code":      200,
		"msg":       "查询成功",
		"count":     count,
		"popeOrder": popeOrder,
	})
	return
}
func PopeOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
			SELECT
		  orders.id AS OrderId,
		  popes.pope_no AS PopeNo,
		  popes.pope_name AS PopeName,
		  popes.pope_area AS PopeArea,
		  popes.pope_user AS PopeUser,
		  orders.order_no AS OrderNo,
		  orders.order_boss AS OrderBoss,
		  orders.order_num AS OrderNum,
		  orders.order_price AS OrderPrice
		FROM
		  orders
		  LEFT JOIN popes ON orders.pid = popes.id
		WHERE
		  orders.id = 1
	*/
	var popeOrder models.PopeOrder
	redis_key := "detail_popeOrder" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderId",
				"popes.pope_no AS PopeNo",
				"popes.pope_name AS PopeName",
				"popes.pope_area AS PopeArea",
				"popes.pope_user AS PopeUser",
				"orders.order_no AS OrderNo",
				"orders.order_boss AS OrderBoss",
				"orders.order_num AS OrderNum",
				"orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN popes ON orders.pid = popes.id").
			Where("orders.id = ?", id).
			Debug().
			Find(&popeOrder)
		jsonData, _ := json.Marshal(popeOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &popeOrder)
	}
	c.JSON(200, gin.H{
		"code":      200,
		"msg":       "查询成功",
		"popeOrder": popeOrder,
	})
	return
}
